/*
 *此文件为所有的正则校验文件
 */
// 正整数
export const isNumber = (str: string) => /^\d+(\.\d{1,2})?$/.test(str)
//  纯数字
export const isNumber2 = (str: string) => /^\d+$/.test(str)
// 数字和字母
export const numberAndLetter = (str: string) => /^[A-Za-z0-9_]+$/.test(str)
// 13位纯数字
export const number13 = (str: string) => /^\d{13}\b/.test(str)
// url
export const urlValidate = (str: string) =>
  /(https?|ftp|file):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/.test(str)

// 密码验证
export const regexPass = (passwordNew: string, temporary: string) => {
  let regex = false
  const lengthRegex = new RegExp('^.{8,18}$') // >= 8位小于18
  const characterRegex = new RegExp('[a-z]') // 小寫
  const characterRegex2 = new RegExp('[A-Z]') // 大寫
  const numberRegex = new RegExp('[0-9]') // 數字
  const chinese = new RegExp('[\\u4E00-\\u9FFF]+', 'g') // 是否有中文
  let num = 0
  temporary = temporary.replace(/[^0-9a-zA-Z]/g, '')
  if (characterRegex.test(passwordNew)) {
    num += 1
  }
  if (characterRegex2.test(passwordNew)) {
    num += 1
  }
  if (numberRegex.test(passwordNew)) {
    num += 1
  }
  if (passwordNew.length - temporary.length > 0) {
    num += 1
  }
  if (num >= 3) {
    regex = true
  } else {
    regex = false
  }
  if (!lengthRegex.test(passwordNew)) {
    regex = false
  }
  if (chinese.test(passwordNew)) {
    regex = false
  }
  const limitArr = ['%', '&', '"', "'", '<', '>', '+', '=']
  for (let i = 0; i < limitArr.length; i++) {
    if (passwordNew.indexOf(limitArr[i]) > -1) {
      regex = false
      break
    }
  }
  return regex
}
